#include <iostream>
#include <vector>
#include <list>
#include <sw/redis++/redis++.h>

using namespace std::chrono_literals;
using namespace sw::redis;

void test_1(Redis &redis)
{
    redis.flushall();
    std::cout << "hset hget" << std::endl;
    redis.hset("key", {std::make_pair("f1", "111"),
                       std::make_pair("f2", "222"),
                       std::make_pair("f3", "333")});
    redis.hset("key", "f4", "444");
    redis.hset("key", std::make_pair("f5", "555"));
    std::vector<std::pair<std::string, std::string>> vec{
        std::make_pair("f6", "666"),
        std::make_pair("f7", "777"),
        std::make_pair("f8", "888")};
    redis.hset("key", vec.begin(), vec.end());

    auto ans = redis.hget("key", "f6");
    if (ans)
        std::cout << ans.value() << std::endl;
    else
        std::cout << "不存在" << std::endl;

    std::vector<std::string> res;
    auto iter = std::back_inserter(res);

    redis.hmget("key", {"f1", "f2", "f5"}, iter);
    for (auto e : res)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_2(Redis &redis)
{
    redis.flushall();
    redis.hset("key", {std::make_pair("f1", "111"),
                       std::make_pair("f2", "222"),
                       std::make_pair("f3", "333")});

    std::vector<std::string> ans;
    auto iter = std::back_inserter(ans);
    redis.hkeys("key", iter);

    for (auto e : ans)
        std::cout << e << " ";
    std::cout << std::endl;

    redis.hvals("key", iter);
    for (auto e : ans)
        std::cout << e << " ";
    std::cout << std::endl;

    bool res = redis.hexists("key", "f1");
    if(res) std::cout << "exists!" << std::endl;
    else std::cout << "not exists!" << std::endl;

}

void test_3(Redis &redis)
{
    redis.flushall();
    redis.hset("key", {std::make_pair("f1", "111"),
                       std::make_pair("f2", "222"),
                       std::make_pair("f3", "333")});
    redis.hdel("key", "f2");
    std::vector<std::string> ans;
    auto iter = std::back_inserter(ans);
    redis.hkeys("key", iter);
    
    for(auto e : ans)
        std::cout << e << " ";
    std::cout << std::endl;

}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    // test_1(redis);
    // test_2(redis);
    test_3(redis);

    return 0;
}